package com.fingard.dsp.bank.directbank.spdb01;


import java.util.Calendar;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.ReqPayRecDetail;
import com.fingard.dsp.bank.reqrespobj.single.detail.ReqQrySglBatPayDetail;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class SPDB01SinglePay extends SPDB01Base implements ISinglePay {

    //单笔支付，不需采用流处理方式
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetDetail;
        if ("6908".equals(p_req1908.reqHeader.transType)) {
            SPDB01RealTime realTime = new SPDB01RealTime();
            realTime.bankFront = this.bankFront;
            realTime.reqHeader = this.reqHeader;
            realTime.sbLog = this.sbLog;
            tmpRetDetail = realTime.realTimePayRec(p_req1908);
        } else {
            tmpRetDetail = d_190802(p_req1908);
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    //190802对外支付
    private RetAtsSglDetail d_190802(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        SPDB01Header tmpBnkHeader = new SPDB01Header();
        tmpBnkHeader.transCode = "8801";
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //需要进行对账码拼接以及节点解析，采用XElement进行处理
        xmlWriter.autoFormat = false;
        XElement tmpXElement = new XElement("body", "");
        tmpXElement.addChild(new XElement("elecChequeNo", p_req1908.singleDetail.askNumber));
        tmpXElement.addChild(new XElement("acctNo", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("acctName", p_req1908.singleDetail.payName));
        tmpXElement.addChild(new XElement("bespeakDate", ""));
        tmpXElement.addChild(new XElement("payeeAcctNo", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("payeeName", p_req1908.singleDetail.recName));

        if (p_req1908.singleDetail.sameBnk.equals("1")) {//同行
            if (p_req1908.singleDetail.isPrivate.equals("1")) {//对私
                //payeeType 收款人账户类型 0-对公账号  1-卡  8-活期存折
                if (p_req1908.singleDetail.cardType.equals("1")) {
                    tmpXElement.addChild(new XElement("payeeType", "8"));
                } else {
                    tmpXElement.addChild(new XElement("payeeType", "1"));
                }
            } else if (p_req1908.singleDetail.isPrivate.equals("2")) {//对公
                tmpXElement.addChild(new XElement("payeeType", "0"));
            } else {
                tmpXElement.addChild(new XElement("payeeType", ""));
            }
            tmpXElement.addChild(new XElement("payeeBankName", ""));
            tmpXElement.addChild(new XElement("payeeAddress", ""));
        } else {
            tmpXElement.addChild(new XElement("payeeType", ""));
            tmpXElement.addChild(new XElement("payeeBankName", p_req1908.singleDetail.getOppBankLocation()));
            tmpXElement.addChild(new XElement("payeeAddress", p_req1908.singleDetail.recAreaName));
        }
        tmpXElement.addChild(new XElement("amount", p_req1908.singleDetail.payAmount));
        if (p_req1908.singleDetail.sameBnk.equals("1")) {//同行
            tmpXElement.addChild(new XElement("sysFlag", "0"));
            tmpXElement.addChild(new XElement("remitLocation", ""));
            //ATSES-22232 通威目前在用的十个支付渠道,对账码识别截取优化-----范天星/2020/01/19
            tmpXElement.addChild(new XElement("note", p_req1908.singleDetail.purpose + "＋" + p_req1908.singleDetail.abstractATS));
            tmpXElement.addChild(new XElement("payeeBankSelectFlag", ""));
            tmpXElement.addChild(new XElement("payeeBankNo", ""));
        } else {
            tmpXElement.addChild(new XElement("sysFlag", "1"));
            boolean isRemitLocation = false;
            //跨行同城非加急，如果设置了票交
            if (p_req1908.singleDetail.isCrossBankSameCityOrdinary() && getBankConfig().getLocalClearings()) {
                tmpXElement.addChild(new XElement("remitLocation", "0"));
            } else {
                tmpXElement.addChild(new XElement("remitLocation", "1"));
                isRemitLocation = true;
            }
            //ATSES-22232 通威目前在用的十个支付渠道,对账码识别截取优化-----范天星/2020/01/19
//            tmpXElement.addChild(new XElement("note", p_req1908.singleDetail.purpose + "＋" + p_req1908.singleDetail.abstractATS));
            //ATSES-26289 浦发银行SPDB01config对账码拼接问题-----范天星/2020/04/27(修复硬编码拼接，通过config文件配置)
            tmpXElement.addChild(new XElement("note", p_req1908.singleDetail.purpose));
            if (StringHelper.hasAnyChar(p_req1908.singleDetail.cnapsCode) && isRemitLocation) {
                tmpXElement.addChild(new XElement("payeeBankSelectFlag", "1"));
                tmpXElement.addChild(new XElement("payeeBankNo", p_req1908.singleDetail.cnapsCode));
            } else {
                tmpXElement.addChild(new XElement("payeeBankSelectFlag", ""));
                tmpXElement.addChild(new XElement("payeeBankNo", ""));
            }
        }

        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());

        xmlWriter.writeXElement(tmpXElement);

        xmlWriter.writeEndDocument();//写入报文完成
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), tmpBnkHeader);//发送银行
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String returnCode = tmpBkReader.getFirstText("/packet/head/returnCode");
                String returnMsg = tmpBkReader.getFirstText("/packet/head/returnMsg");
                if (StringHelper.hasAnyChar(returnCode)) {
                    retDetail.respCode = "0";
                    String bodyCode = tmpBkReader.getFirstText("/packet/body/returnCode");
                    String bodyMsg = tmpBkReader.getFirstText("/packet/body/returnMsg");
                    String bodyTransStatus = tmpBkReader.getFirstText("/packet/body/transStatus");

                    if (returnCode.equals("AAAAAAA") && bodyTransStatus != null) {
                        retDetail.transState = bodyTransStatus;
                    } else if (bodyTransStatus != null) {
                        retDetail.transState = returnCode + "+" + bodyTransStatus;
                    } else if (returnCode.length() > 1) {//为了不跟原先1位的Sy_PayStateConfig冲突，加上位数判断
                        if (bodyCode != null && !returnCode.equals(bodyCode)) {
                            retDetail.transState = returnCode + "+" + bodyCode;
                        } else {
                            retDetail.transState = returnCode;
                        }
                        //这时赋值PayInfoCode，其它情况，直接用中文去映射统一信息码，因为浦发无码返回
                        retDetail.payInfoCode = retDetail.transState;
                    }
                    retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                    if (StringHelper.hasAnyChar(bodyMsg)) {
                        retDetail.respInfo = bodyMsg;
                    } else {
                        retDetail.respInfo = returnMsg;
                    }
                    if (StringHelper.isNullOrEmpty(retDetail.payInfoCode)) {
                        retDetail.payInfoCode = retDetail.respInfo;
                    }
                    retDetail.outTransID = tmpBkReader.getFirstText("/packet/body/acceptNo");
                } else {
                    retDetail.respCode = "-1";
                    if (StringHelper.hasAnyChar(returnMsg)) {
                        retDetail.respInfo = returnMsg;
                    } else {
                        retDetail.respInfo = "returnCode节点为空";
                    }
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } /*else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        }*/ else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }
        return retDetail;
    }

    //1944 状态查询
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {

        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1944.reqHeader);
        if ("6944".equals(p_req1944.reqHeader.transType)) {
            SPDB01RealTime realTime = new SPDB01RealTime();
            realTime.bankFront = this.bankFront;
            realTime.reqHeader = this.reqHeader;
            realTime.sbLog = this.sbLog;
            retAtsSingle = realTime.qrySglPay(p_req1944);
        } else {
            retAtsSingle = qrySglPay(p_req1944);
        }

        return retAtsSingle;
    }


    private RetAtsSingle qrySglPay(ReqQrySingle p_req1944) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail();
        try {
            SPDB01Header tmpBnkHeader = new SPDB01Header();
            tmpBnkHeader.transCode = "8804";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            xmlWriter.autoFormat = false;
            xmlWriter.writeStartElement("body");
            xmlWriter.writeElementString("elecChequeNo", p_req1944.askNumber);
            xmlWriter.writeElementString("acctNo", p_req1944.payAct);

            Calendar tmpBGNDAT = Calendar.getInstance();
            tmpBGNDAT.setTime(p_req1944.getBgnDateTime());
            tmpBGNDAT.add(Calendar.DAY_OF_MONTH, -1);

            xmlWriter.writeElementString("beginDate", Format.Date8Format.format(tmpBGNDAT.getTime()));
            xmlWriter.writeElementString("endDate", Format.Date8Format.format(new Date()));
            xmlWriter.writeElementClosed("acceptNo");
            xmlWriter.writeElementClosed("serialNo");
            xmlWriter.writeElementString("queryNumber", "10");
            xmlWriter.writeElementString("beginNumber", "1");
            xmlWriter.writeElementString("singleOrBatchFlag", "0");
            xmlWriter.writeEndDocument();

            String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString(), tmpBnkHeader);
            if (tmpRetBkStr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String returnCode = tmpBkReader.getFirstText("/packet/head/returnCode");
                    String returnMsg = tmpBkReader.getFirstText("/packet/head/returnMsg");
                    if (StringHelper.isNullOrEmpty(returnMsg)) {
                        returnMsg = tmpBkReader.getFirstText("/packet/body/returnMsg");
                    }
                    if ("AAAAAAA".equals(returnCode)) {
                        String bkTotalCount = tmpBkReader.getFirstText("/packet/body/totalCount");
                        int totalCount = -1;
                        if (StringHelper.hasAnyChar(bkTotalCount)) {
                            totalCount = Integer.parseInt(bkTotalCount);
                        }
                        if (tmpBkReader.read("/packet/body/lists/list")) {
                            tmpRetAtsDetail.respCode = "0";
                            String elecChequeNo = tmpBkReader.readedXElement.getSingleText("elecChequeNo");
                            tmpRetAtsDetail.reqSeqID = elecChequeNo;
                            if (!p_req1944.askNumber.equals(elecChequeNo)) {
                                tmpRetAtsDetail.respCode = "-1";
                                tmpRetAtsDetail.respInfo = "银行返回的支付流水号" + elecChequeNo + "与查询支付流水号" + p_req1944.askNumber + "不符";
                            }
                            tmpRetAtsDetail.transState = tmpBkReader.readedXElement.getSingleText("transStatus");
                            tmpRetAtsDetail.transAmt = tmpBkReader.readedXElement.getSingleText("amount");
                            tmpRetAtsDetail.outTransID = tmpBkReader.readedXElement.getSingleText("acceptNo");

                            String note = tmpBkReader.readedXElement.getSingleText("note");
                            if (StringHelper.hasAnyChar(note)) {
                                String infoCode = note;
                                int indexErr = note.lastIndexOf("错误原因:");
                                if (indexErr >= 0) {
                                    if (note.length() >= indexErr + 5 + 7) {
                                        infoCode = note.substring(indexErr + 5, indexErr + 12);
                                        tmpRetAtsDetail.payInfoCode = infoCode;
                                    }
                                    tmpRetAtsDetail.respInfo = note;
                                }
                            }
                        } else if (totalCount == 0) {
                            if (!tmpRetBkStr[1].contains(p_req1944.askNumber) && !tmpRetBkStr[1].contains("<list>") && !tmpRetBkStr[1].contains("</list>")) {
                                if (canBeNotExist(p_req1944, tmpRetAtsDetail, 30)) {
                                    tmpRetAtsDetail.respCode = "0";
                                    tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
                                    tmpRetAtsDetail.transState = PaymentState.FAIL_Payment;
                                    tmpRetAtsDetail.payInfoCode = FailCode.E1302;
                                    tmpRetAtsDetail.respInfo = FailCode.E1302_DESC;
                                }
                            } else {
                                tmpRetAtsDetail.respCode = "-1";
                                tmpRetAtsDetail.respInfo = "银行未处理完成";
                            }
                        } else {
                            tmpRetAtsDetail.respCode = "-1";
                            tmpRetAtsDetail.respInfo = "totalCount节点为空";
                        }
                    } else {
                        tmpRetAtsDetail.respCode = "-1";
                        tmpRetAtsDetail.respInfo = returnMsg;
                    }
                } else {
                    tmpRetAtsDetail.respCode = "-1";
                    tmpRetAtsDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetAtsDetail.respCode = "-1";
                tmpRetAtsDetail.respInfo = tmpRetBkStr[1];
            }
        } catch (Exception e) {
            tmpRetAtsDetail.respCode = "-1";
            tmpRetAtsDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }

        retAts.writeRetDetail(tmpRetAtsDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        RetAtsSingleBat retAts = new RetAtsSingleBat(p_req1908BAT.reqHeader);
        retAts.totalAmt = p_req1908BAT.totalAmt;
        retAts.totalNum = p_req1908BAT.totalAmt;
        SPDB01Header tmpBnkHeader = new SPDB01Header();
        tmpBnkHeader.transCode = "8800";
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //需要进行对账码拼接以及节点解析，采用XElement进行处理
        xmlWriter.autoFormat = false;
        xmlWriter.writeStartElement("body");
        xmlWriter.writeElementString("totalNumber", p_req1908BAT.totalNum);
        xmlWriter.writeElementString("totalAmount", p_req1908BAT.totalAmt);
        xmlWriter.writeStartElementByAttr("lists", "<lists name=\"PayList\">");
        while (p_req1908BAT.readRecord()) {
            XElement tmpXElement = new XElement("list", "");
            ReqPayRecDetail tmpDetail = p_req1908BAT.readedDetail;
            tmpXElement.addChild(new XElement("elecChequeNo", tmpDetail.askNumber));
            tmpXElement.addChild(new XElement("acctNo", tmpDetail.payAct));
            tmpXElement.addChild(new XElement("acctName", tmpDetail.payName));
            tmpXElement.addChild(new XElement("bespeakDate", ""));
            tmpXElement.addChild(new XElement("payeeAcctNo", tmpDetail.recAct));
            tmpXElement.addChild(new XElement("payeeName", tmpDetail.recName));

            if (tmpDetail.sameBnk.equals("1")) {//同行
                if (tmpDetail.isPrivate.equals("1")) {//对私
                    //payeeType 收款人账户类型 0-对公账号  1-卡  8-活期存折
                    if (tmpDetail.cardType.equals("1")) {
                        tmpXElement.addChild(new XElement("payeeType", "8"));
                    } else {
                        tmpXElement.addChild(new XElement("payeeType", "1"));
                    }
                } else if (tmpDetail.isPrivate.equals("2")) {//对公
                    tmpXElement.addChild(new XElement("payeeType", "0"));
                } else {
                    tmpXElement.addChild(new XElement("payeeType", ""));
                }
                tmpXElement.addChild(new XElement("payeeBankName", ""));
                tmpXElement.addChild(new XElement("payeeAddress", ""));
            } else {
                tmpXElement.addChild(new XElement("payeeType", ""));
                tmpXElement.addChild(new XElement("payeeBankName", tmpDetail.getOppBankLocation()));
                tmpXElement.addChild(new XElement("payeeAddress", tmpDetail.recAreaName));
            }
            tmpXElement.addChild(new XElement("amount", tmpDetail.payAmount));
            if (tmpDetail.sameBnk.equals("1")) {//同行
                tmpXElement.addChild(new XElement("sysFlag", "0"));
                tmpXElement.addChild(new XElement("remitLocation", ""));
                tmpXElement.addChild(new XElement("note", tmpDetail.purpose + "＋" + tmpDetail.abstractATS));
                tmpXElement.addChild(new XElement("payeeBankSelectFlag", ""));
                tmpXElement.addChild(new XElement("payeeBankNo", ""));
            } else {
                tmpXElement.addChild(new XElement("sysFlag", "1"));
                //跨行同城非加急，如果设置了票交
                if (tmpDetail.isCrossBankSameCityOrdinary() && getBankConfig().getLocalClearings()) {
                    tmpXElement.addChild(new XElement("remitLocation", "0"));
                } else {
                    tmpXElement.addChild(new XElement("remitLocation", "1"));
                }
                tmpXElement.addChild(new XElement("note", tmpDetail.purpose));
                if (StringHelper.hasAnyChar(tmpDetail.cnapsCode)) {
                    tmpXElement.addChild(new XElement("payeeBankSelectFlag", "1"));
                    tmpXElement.addChild(new XElement("payeeBankNo", tmpDetail.cnapsCode));
                } else {
                    tmpXElement.addChild(new XElement("payeeBankSelectFlag", ""));
                    tmpXElement.addChild(new XElement("payeeBankNo", ""));
                }
            }

            nodeAnalysis(tmpDetail.srcXElement, tmpXElement, getCharset());

            xmlWriter.writeXElement(tmpXElement);
        }

        xmlWriter.writeEndDocument();//写入报文完成
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), tmpBnkHeader);//发送银行
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String returnCode = tmpBkReader.getFirstText("/packet/head/returnCode");
                String returnMsg = tmpBkReader.getFirstText("/packet/head/returnMsg");
                if (StringHelper.hasAnyChar(returnCode)) {
                    retAts.batRespCode = "0";
                    retAts.batTransState = returnCode;
                    retAts.batPayInfoCode = retAts.batTransState;
                    retAts.reqBatchNO = p_req1908BAT.reqBatchNO;
                    retAts.batRespInfo = returnMsg;

                    if (StringHelper.isNullOrEmpty(retAts.batPayInfoCode)) {
                        retAts.batPayInfoCode = retAts.batRespInfo;
                    }
                } else {
                    retAts.batRespCode = "-1";
                    if (StringHelper.hasAnyChar(returnMsg)) {
                        retAts.batRespInfo = returnMsg;
                    } else {
                        retAts.batRespInfo = "returnCode节点为空";
                    }
                }
            } else {
                retAts.batRespCode = "-1";
                retAts.batRespInfo = tmpSbMsg.toString();
            }
        } /*else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retAts.batRespCode = "0";
            retAts.batRespInfo = tmpStrRet[1];
            retAts.reqBatchNO = p_req1908BAT.reqBatchNO;
            retAts.batTransState = PaymentState.FAIL_Payment;
            retAts.batPayInfoCode = tmpStrRet[0];
        }*/ else {
            retAts.batRespCode = "-1";
            retAts.batRespInfo = tmpStrRet[1];
        }
        retAts.writeAfterDetail();
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        RetAtsSingleBat retAts = new RetAtsSingleBat(p_req1944BAT.reqHeader);
        retAts.reqBatchNO = p_req1944BAT.reqBatchNO;
        SPDB01Header tmpBnkHeader = new SPDB01Header();
        tmpBnkHeader.transCode = "8804";
        while (p_req1944BAT.readRecord()) {
            RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail();
            ReqQrySglBatPayDetail tmpDetail = p_req1944BAT.readedDetail;
            tmpRetAtsDetail.reqSeqID = tmpDetail.reqSeqID;
            try {
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                xmlWriter.autoFormat = false;
                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("elecChequeNo", tmpDetail.reqSeqID);
                xmlWriter.writeElementString("acctNo", tmpDetail.payAct);

                Calendar tmpBGNDAT = Calendar.getInstance();
                tmpBGNDAT.setTime(tmpDetail.getBgnDateTime());
                tmpBGNDAT.add(Calendar.DAY_OF_MONTH, -1);

                xmlWriter.writeElementString("beginDate", Format.Date8Format.format(tmpBGNDAT.getTime()));
                xmlWriter.writeElementString("endDate", Format.Date8Format.format(new Date()));
                xmlWriter.writeElementClosed("acceptNo");
                xmlWriter.writeElementClosed("serialNo");
                xmlWriter.writeElementString("queryNumber", "10");
                xmlWriter.writeElementString("beginNumber", "1");
                xmlWriter.writeElementString("singleOrBatchFlag", "1");
                xmlWriter.writeEndDocument();

                String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString(), tmpBnkHeader);
                if (tmpRetBkStr[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        retAts.batRespCode = "0";
                        String returnCode = tmpBkReader.getFirstText("/packet/head/returnCode");
                        String returnMsg = tmpBkReader.getFirstText("/packet/head/returnMsg");
                        if (returnCode != null && returnCode.equals("AAAAAAA")) {
                            String bkTotalCount = tmpBkReader.getFirstText("/packet/body/totalCount");
                            int totalCount = -1;
                            if (StringHelper.hasAnyChar(bkTotalCount)) {
                                totalCount = Integer.parseInt(bkTotalCount);
                            }
                            if (tmpBkReader.read("/packet/body/lists/list")) {
                                tmpRetAtsDetail.respCode = "0";
                                String elecChequeNo = tmpBkReader.readedXElement.getSingleText("elecChequeNo");
                                tmpRetAtsDetail.reqSeqID = elecChequeNo;
                                if (!tmpDetail.reqSeqID.equals(elecChequeNo)) {
                                    tmpRetAtsDetail.respCode = "-1";
                                    tmpRetAtsDetail.respInfo = "银行返回的支付流水号" + elecChequeNo + "与查询支付流水号" + tmpDetail.reqSeqID + "不符";
                                }
                                tmpRetAtsDetail.transState = tmpBkReader.readedXElement.getSingleText("transStatus");
                                tmpRetAtsDetail.transAmt = tmpBkReader.readedXElement.getSingleText("amount");

                                String note = tmpBkReader.readedXElement.getSingleText("note");

                                if (StringHelper.hasAnyChar(note)) {
                                    String infoCode = note;
                                    int indexErr = note.lastIndexOf("错误原因:");
                                    if (indexErr >= 0) {
                                        if (note.length() >= indexErr + 5 + 7) {
                                            infoCode = note.substring(indexErr + 5, indexErr + 12);
                                            tmpRetAtsDetail.payInfoCode = infoCode;
                                        }
                                        tmpRetAtsDetail.respInfo = note;
                                    }
                                }
                            } else if (totalCount == 0) {
                                tmpRetAtsDetail.respCode = "-1";
                                tmpRetAtsDetail.respInfo = "银行未处理完成";
                            } else {
                                tmpRetAtsDetail.respCode = "-1";
                                tmpRetAtsDetail.respInfo = "totalCount节点为空";
                            }
                        } else {
                            tmpRetAtsDetail.respCode = "-1";
                            tmpRetAtsDetail.respInfo = returnMsg;
                        }
                    } else {
                        tmpRetAtsDetail.respCode = "-1";
                        tmpRetAtsDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRetAtsDetail.respCode = "-1";
                    tmpRetAtsDetail.respInfo = tmpRetBkStr[1];
                }
            } catch (Exception ex) {
                tmpRetAtsDetail.respCode = "-1";
                tmpRetAtsDetail.respInfo = ex.getMessage();
            }
            retAts.writeRetDetail(tmpRetAtsDetail);
        }

        retAts.writeAfterDetail();
        return retAts;
    }
}
